﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;

namespace WishHand.Core
{
   public class ImageDownClient
    {
        public static BitMapResult DownLoadImage(string url,string path,string name,bool isDetail =false)
        {
            WebClient webClient = new WebClient();

            try
            {
                webClient.DownloadFile(new Uri(url), path + name);

            }
            catch (Exception)
            {

                //throw;
            }
            
            var bitmap = new FileInfo(path + name);
            BitMapResult bitMapResult = new BitMapResult();
            if (bitmap.Length>300 *1024 && isDetail)
            {
                bitMapResult.IsBig = true;


                long imageSize = bitmap.Length;

                decimal times= Math.Ceiling( Convert.ToDecimal((decimal)imageSize / (decimal)(300* 1024)));  //cishu 

                Bitmap resultImage = new Bitmap(path + name);
                using (resultImage)
                {
                    for (int i = 0; i < times; i++)
                    {
                        int oneHeight = Convert.ToInt32(resultImage.Height / times);
                        if (i==times-1)
                        {
                            oneHeight = resultImage.Height - oneHeight * (int)(times-1);
                        }
                        Bitmap b = new Bitmap(resultImage.Width, oneHeight); //一张图
                        try
                        {
                            using (Graphics g = Graphics.FromImage(b))
                            {
                                //清空画布并以透明背景色填充
                                g.Clear(Color.Transparent);


                                //在指定位置并且按指定大小绘制原图片的指定部分
                                g.DrawImage(resultImage, new Rectangle(0, 0, resultImage.Width, oneHeight), 0, oneHeight*i, b.Width, b.Height, GraphicsUnit.Pixel);
                                string[] nameArr = name.Split('.');

                                string newName = $"{nameArr[0]}-{i}.jpg";

                                g.Save();
                                b.Save(path + newName);
                                bitMapResult.result.Add(path + newName);
                            }
                        }
                        catch (System.Exception e)
                        {

                            Console.WriteLine("异常");
                        }
                        finally
                        {
                            b.Dispose();
                        }

                    }
                }
               

            }
            else
            {
                bitMapResult.IsBig = false;
            }


            return bitMapResult;
        }


    }

    public class BitMapResult {

        public IList<string> result { get; set; }

        public bool IsBig { get; set; }

        public BitMapResult()
        {
            result = new List<string>();
        }
    }
}
